﻿Public Class frmUserStoriesModify


    Dim strNameForm As String = "Modify User Story"
    Dim mID As Integer

    Public Function Initialize(ByVal inID As Integer) As Boolean
        Try
            Me.Text = strNameForm
            mID = inID

            cboSprints.Load("Select * From Sprints", "Name", gConnectionStringAgenda.ToString)
            cboProjects.Load("Select * From Projects", "Name", gConnectionStringAgenda.ToString)
            Dim listStatus As New List(Of Base.GlobalConst)
            listStatus.Add(US_STATUS_PLANNED)
            listStatus.Add(US_STATUS_ONGOING)
            listStatus.Add(US_STATUS_ONHOLD)
            listStatus.Add(US_STATUS_DONE)
            listStatus.Add(US_STATUS_CANCELLED)
            listStatus.Add(US_STATUS_NEXT)
            cboStatus.DataSource = listStatus
            cboStatus.ValueMember = "NUMBER"
            cboStatus.DisplayMember = "NAME"
            listStatus = Nothing

            AddHandler cboSprints.OnInsert, AddressOf InterfaceManager.Sprints_OnInsert
            AddHandler cboSprints.OnModify, AddressOf InterfaceManager.Sprints_OnModify
            AddHandler cboProjects.OnInsert, AddressOf InterfaceManager.Projects_OnInsert
            AddHandler cboProjects.OnModify, AddressOf InterfaceManager.Projects_OnModify

            If Not LoadForm() Then
                MsgBox("Impossible open form.", vbExclamation, Me.Text)
                Return False
            End If
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "LoadForm"

    Private Function LoadForm() As Boolean
        Try
            Dim oTUserStories As New Data.TUserStories
            If oTUserStories.LoadData(mID) Then
                txtNumber.Text = oTUserStories.mName
                txtNumber.Text = oTUserStories.mNumber
                txtDescription.Text = oTUserStories.mDescription
                txtDone.Text = oTUserStories.mDone
                txtPriority.Text = oTUserStories.mPriority
                cboProjects.SelectedIndex = oTUserStories.mProjectsID
                cboSprints.SelectedIndex = oTUserStories.mSprintsID
                cboStatus.SelectedIndex = oTUserStories.mStatus

            Else
                Return False
            End If
            oTUserStories = Nothing
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Load data", Me.Name, "LoadForm", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModify.Click
        Try
            If IsComplete() Then
                Dim oTUserStories As New Data.TUserStories
                If oTUserStories.LoadData(mID) Then
                    oTUserStories.mName = txtNumber.Text
                    oTUserStories.mNumber = txtNumber.Text
                    oTUserStories.mDescription = txtDescription.Text
                    oTUserStories.mDone = txtDone.Text
                    oTUserStories.mPriority = txtPriority.GetIntNumber
                    oTUserStories.mProjectsID = cboProjects.SelectedValue
                    oTUserStories.mSprintsID = cboSprints.SelectedValue
                    oTUserStories.mStatus = cboStatus.SelectedValue

                    If oTUserStories.Modify() Then
                        Me.DialogResult = Windows.Forms.DialogResult.OK
                    Else
                        MsgBox("Impossible modify the user story.", vbExclamation, Me.Text)
                        Me.DialogResult = Windows.Forms.DialogResult.Cancel
                    End If
                Else
                    MsgBox("Impossible modify the user story.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "cmdModify_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtNumber.Text = "" Then
                MsgBox("Insert number.", vbExclamation, Me.Name)
                txtDescription.Focus()
                Return False
            End If
            If Not cboSprints.IsItemSelected Then
                MsgBox("Select sprint.", vbExclamation, Me.Name)
                cboSprints.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub frmTableSingleInsertion_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class